
package spikes_goods_sku

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/spikes_goods_sku"
    spikes_goods_skuReq "github.com/flipped-aurora/gin-vue-admin/server/model/spikes_goods_sku/request"
)

type SpikesGoodsSkusService struct {}
// CreateSpikesGoodsSkus 创建spikesGoodsSkus表记录
// Author [yourname](https://github.com/yourname)
func (spikesGoodsSkusService *SpikesGoodsSkusService) CreateSpikesGoodsSkus(spikesGoodsSkus *spikes_goods_sku.SpikesGoodsSkus) (err error) {
	err = global.GVA_DB.Create(spikesGoodsSkus).Error
	return err
}

// DeleteSpikesGoodsSkus 删除spikesGoodsSkus表记录
// Author [yourname](https://github.com/yourname)
func (spikesGoodsSkusService *SpikesGoodsSkusService)DeleteSpikesGoodsSkus(ID string) (err error) {
	err = global.GVA_DB.Delete(&spikes_goods_sku.SpikesGoodsSkus{},"id = ?",ID).Error
	return err
}

// DeleteSpikesGoodsSkusByIds 批量删除spikesGoodsSkus表记录
// Author [yourname](https://github.com/yourname)
func (spikesGoodsSkusService *SpikesGoodsSkusService)DeleteSpikesGoodsSkusByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]spikes_goods_sku.SpikesGoodsSkus{},"id in ?",IDs).Error
	return err
}

// UpdateSpikesGoodsSkus 更新spikesGoodsSkus表记录
// Author [yourname](https://github.com/yourname)
func (spikesGoodsSkusService *SpikesGoodsSkusService)UpdateSpikesGoodsSkus(spikesGoodsSkus spikes_goods_sku.SpikesGoodsSkus) (err error) {
	err = global.GVA_DB.Model(&spikes_goods_sku.SpikesGoodsSkus{}).Where("id = ?",spikesGoodsSkus.ID).Updates(&spikesGoodsSkus).Error
	return err
}

// GetSpikesGoodsSkus 根据ID获取spikesGoodsSkus表记录
// Author [yourname](https://github.com/yourname)
func (spikesGoodsSkusService *SpikesGoodsSkusService)GetSpikesGoodsSkus(ID string) (spikesGoodsSkus spikes_goods_sku.SpikesGoodsSkus, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&spikesGoodsSkus).Error
	return
}
// GetSpikesGoodsSkusInfoList 分页获取spikesGoodsSkus表记录
// Author [yourname](https://github.com/yourname)
func (spikesGoodsSkusService *SpikesGoodsSkusService)GetSpikesGoodsSkusInfoList(info spikes_goods_skuReq.SpikesGoodsSkusSearch) (list []spikes_goods_sku.SpikesGoodsSkus, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&spikes_goods_sku.SpikesGoodsSkus{})
    var spikesGoodsSkuss []spikes_goods_sku.SpikesGoodsSkus
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&spikesGoodsSkuss).Error
	return  spikesGoodsSkuss, total, err
}
func (spikesGoodsSkusService *SpikesGoodsSkusService)GetSpikesGoodsSkusPublic() {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
